//
//  SSHomeOriginalView.swift
//  SSSina
//
//  Created by ss on 2017/7/4.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit
import SnapKit
class SSHomeOriginalView: UIView {

    
    //记录原创微博的底部约束
    var originalViewBottomConstraint:Constraint?
    
    //提供一个模型属性,供外界赋值
    var statusViewModel:SSStatusViewModel?{
        didSet{
            //赋值
            headImageView.ss_setImage(withUrlSting: statusViewModel?.homeModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            nameLabel.text = statusViewModel?.homeModel?.user?.name
            verifiedImageView.image = statusViewModel?.verifiedImage
            mbrankImageView.image = statusViewModel?.mbrankImage
            contentLabel.text = statusViewModel?.homeModel?.text
        
            // 卸载约束
            originalViewBottomConstraint?.deactivate()
            // 判断是否有配图
            // 如果有
            if let picUrls = statusViewModel?.homeModel?.pic_urls, picUrls.count > 0 {
                
                // 赋值
                pictureView.picUrlsList = picUrls
                
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                // 显示配图
                pictureView.isHidden = false
                
            }else{
                // 没有配图
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.isHidden = true
            }
            
            // 时间
            timeLable.text = statusViewModel?.createdAtText
            // 来源
            sourceLable.text = statusViewModel?.sourceText

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        headImageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.size.equalTo(CGSize(width:35,height:35))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp.right).offset(10)
        }
        mbrankImageView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        timeLable.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLable.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLable)
            make.left.equalTo(timeLable.snp.right).offset(10)
        }
        verifiedImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.right)
            make.centerY.equalTo(headImageView.snp.bottom)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(10)
        }
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
        }
        snp.makeConstraints { (make) in
           originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
        }
    
    }

    //懒加载控件
    //头像
    private lazy var headImageView = UIImageView(named: "avatar_default_big")
    //昵称
    private lazy var nameLabel:UILabel = UILabel(text: "昵称", textColor: UIColor.black, fontSize: SSNormalFontSize )
   //会员等级
    private lazy var mbrankImageView:UIImageView = UIImageView(named: "common_icon_membership")
    //时间
    private lazy var timeLable:UILabel = UILabel(text: "刚刚", textColor: SSThemeColor, fontSize: SSSmallFontSize)
    //来源
    private lazy var sourceLable:UILabel = UILabel(text: "来自", textColor: SSThemeColor, fontSize: SSSmallFontSize)
    //认证
    private lazy var verifiedImageView:UIImageView = UIImageView(named: "avatar_vgirl")
    //微博正文
    private lazy var contentLabel:UILabel = UILabel(text: "", textColor: UIColor.black, fontSize: SSNormalFontSize, maxWidth:SSSCREENW - 20)
    //配图
    private lazy var pictureView:SSHomePictureView = SSHomePictureView()
}
